package com.czl.module_service.viewmodel

import androidx.databinding.ObservableBoolean
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.ListBean
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import com.czl.module_service.event.SortBeanEvent
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * @author by xubin,
 * @date on 2022/1/19.
 * PS: Not easy to write code, please indicate.
 */
class SortManagementViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var currentPage = 1
    val uc = UiChangeEvent()
    private val changeObs = ObservableBoolean(true)
    var searchName: String? = null

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<ListBean<SortBean>> = SingleLiveEvent()
        val touchEditEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getSortList(sortName = searchName)
    })

    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 1
        getSortList()
    })

    val search: BindingCommand<Void> = BindingCommand(BindingAction {
        showLoading()
        currentPage = 1
        getSortList(sortName = searchName)
    })

    /**
     * 添加物品
     */
    val addSortClickCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        LiveEventBus.get(SortBeanEvent::class.java).post(SortBeanEvent(null))
        startContainerActivity(AppConstants.Router.Service.F_SERVICE_SORT_ADD)
    })

    override fun setToolbarRightClick() {
        super.setToolbarRightClick()
        changeObs.set(!changeObs.get())
        if (changeObs.get()) {
            toolbarRightText.set("删除")
        } else {
            toolbarRightText.set("取消")
        }
        uc.touchEditEvent.postValue(changeObs.get())
    }

    private fun getSortList(
        priceInfo: Int? = 1,
        companyId: Int? = null,
        sortName: String? = null,
        sortPIdList: List<Int>? = null
    ) {
        model.mTengYunHttpDataSource.getSortList(
            GsonUtils.toJson(
                mapOf(
                    "isPasteCode" to null,
                    "pageNo" to currentPage,
                    "sortName" to sortName,
                    "companyId" to companyId,
                    "priceInfo" to priceInfo,
                    "sortPIdList" to sortPIdList
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<SortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<SortBean>>) {
                    if (t.state == 0) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }
}